<template>
  <div >
    <van-row class="head-label" type="flex" justify="space-between" align="center">
      <van-col>{{ $t('asset_name') }}</van-col>
      <van-col>{{ $t('price_') }}</van-col>
      <van-col>{{ $t('up_down') }}</van-col>
    </van-row>
    <div class="list">
      <template v-if="tickerList.length > 0">
        <van-swipe-cell v-for="item in tickerList" :key="item.id" class="ticker-item" disabled>
          <van-cell>
            <van-row type="flex" justify="space-between" align="center">
              <van-col>
                <div class="stock">{{ item.coin }}<i>/USDT</i></div>
                <div class="price">24H {{ item.volume }}</div>
              </van-col>
              <van-col class="col-right center">
                <div class="price_usd">{{ item.price_usd }}</div>
                <div class="price_cny">￥{{ item.price_cny }}</div>
              </van-col>
              <van-col class="col-right">
                <div class="button" :class="item.change < 0 ? 'hq-red' : 'hq-green'">
                  {{ item.change > 0 ? '+' + item.change + '%' : '' + item.change + '%' }}
                </div>
              </van-col>
            </van-row>
          </van-cell>
          <template #right>
            <van-button square type="danger" :text="$t('actions.del')" />
          </template>
        </van-swipe-cell>
      </template>
      <van-empty v-else :description="$t('empty.default')" />
    </div>
    <ticker-drawer ref="drawer" />
  </div>
</template>

<script>
import { SwipeCell } from 'vant'
import { mapState, mapActions } from 'vuex'
import TickerDrawer from '../components/TickerDrawer'
let timer = null
let isDestroy = false
export default {
  layout: 'navigation',
  components: {
    [SwipeCell.name]: SwipeCell,
    TickerDrawer
  },
  data () {
    return {
      isLoading: false,
      typeShow: 1
    }
  },
  computed: {
    ...mapState({
      currency: ({ currency }) => currency,
      tickerList: ({ ticker }) => ticker.list
    })
  },
  created () {
    isDestroy = false
    this.loadData()
    this.$once('hook:beforeDestroy', () => {
      isDestroy = true
      clearTimeout(timer)
    })
  },
  methods: {
    ...mapActions({
      getTickerList: 'ticker/getTickerList'
    }),
    loadData () {
      this.getTickerList({
        currency: this.currency.name,
        order: 'price',
        order_type: 'desc'
      }).finally(() => {
        if (isDestroy) {
          return
        }
        clearTimeout(timer)
        timer = setTimeout(() => {
          this.loadData()
        }, 1000)
      })
    }
  }
}
</script>

<style scoped lang="less">
.head-label{
    padding: 0 12px 14px 12px;
    font-size: 13px;
    font-weight: 400;
    color: #000000;
  }
.list{
    /deep/.van-swipe-cell__right{
        right: -2px;
    }
    .van-cell{
      padding: 0 12px;
      height: 94px;
      background: #FFFFFF;
      border-radius: 12px;
      margin-bottom: 8px;
      &:after{
        display: none;
      }
      .van-cell__value {
        display: flex;
        align-items: center;
        .van-row{
          width: 100%;
          .van-col{
            flex: 1;
            line-height: 1;
            &.col-right{
              text-align: right;
            }
            &.center{
              box-sizing: content-box;
            }
            .stock{
              font-size: 20px;
              font-weight: bold;
              color: #F5A623;
              i{
                font-size: 13px;
                font-weight: 400;
                color: #3871F9;
                font-style: normal;
              }
            }
            .price{
              margin-top: 10px;
              font-size: 13px;
              font-weight: 400;
              color: #727383;
            }
            .price_usd{
              padding-right: 10px;
              font-size: 18px;
              font-weight: bold;
              color: #3871F9;
            }
            .price_cny{
              padding-right: 10px;
              margin-top: 10px;
              font-size: 13px;
              font-weight: 400;
              color: #727383;
            }
            .button{
              padding: 0 10px;
              height: 28px;
              line-height: 28px;
              display: inline-block;
              text-align: center;
              font-size: 13px;
              font-weight: 500;
              color: #FFFFFF;
              border-radius: 15px;
              &.hq-red{
                background: #FF5D5D;
              }
              &.hq-green{
                background: #23D7AF;
              }
            }
            .floatPrice{
              margin-top: 5px;
              font-size: 13px;
              font-weight: 500;
              &.hq-red{
                color: #FF5D5D;
              }
              &.hq-green{
                color: #23D7AF;
              }
            }
          }
        }
      }
    }
  }
</style>
